<template>
	<view class="j_cont">

		<view class="j_forms">
			<u-form :model="form" ref="uForm">
				<!-- 手机号 -->
				<u-form-item prop="username">
					<u-input v-model="form.username" placeholder="请输入手机号" />
				</u-form-item>
				<!-- 验证码 -->
				<u-form-item prop="Verification" class="Verification">
					<u-input v-model="form.Verification" placeholder="请输入验证码" />
					<!-- 获取验证码 -->
					<view class="j_Verification">
						<u-toast ref="uToast"></u-toast>
						<u-verification-code :seconds="seconds" @end="end" @start="start" ref="uCode"
							@change="codeChange"></u-verification-code>
						<text @tap="getCode">{{tips}}</text>
					</view>
				</u-form-item>
			</u-form>
		</view>
		<view class="j_button">
			<u-button @click="submit" type="warning">下一步</u-button>
		</view>
		<!-- 消息弹出提示 -->
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone:Number,
				//验证码值
				tips: '',
				// refCode: null,时间
				seconds: 10,
				form: {
					//手机号
					username: '',
					//验证码
					Verification: '',
				},
				//表单校验规则
				rules: {
					username: [{
							min: 6,
							message: '请输入正确手机号',
							trigger: 'change'
						},
						{
							validator: (rule, value, callback) => {
								return this.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							trigger: ['change', 'blur'],
						}
					],
					//验证码校验
					Verification: [{
						required: true,
						len: 4,
						message: '请输入验证码',
						trigger: 'change'
					}]
				}
			};
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			submit() {
				// console.log(this.form.username)
				if (this.form.Verification === this.yan) {
					this.$refs.uForm.validate(valid => {
						if (valid) {
							this.$api.postlogins({
								username: this.form.username
							}).then(res => {
								if (res.code == 200) {
									uni.setStorageSync('token', res.data.token)
									uni.reLaunch({
										url: `/pages/login/newPassword/newPassword`
									})
								}
							})
						}
					});
				} else {
					this.$refs.uToast.show({
						title: '验证码错误',
					})
				}
			},
			//验证码
			huoyan() {
				this.yan =
					`${Math.floor(Math.random()*10)}${Math.floor(Math.random()*10)}${Math.floor(Math.random()*10)}${Math.floor(Math.random()*10)}`
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					this.huoyan()
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$u.toast('验证码' + this.yan);
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 1000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
		},
	};
</script>

<style lang="scss" scoped>
	.j_cont {
		width: 100%;
		height: 94vh;
		background: #F2F2F2;
		padding-top: 40upx;
	}

	.j_button {
		width: 100%;
		height: 100upx;
		margin-top: 40upx;
	}

	//表单验证
	.j_forms {
		padding: 0 20upx 0 20upx;
		box-sizing: border-box;
		background: white;

		//获取验证码
		.Verification {
			position: relative;

			//获取验证码
			.j_Verification {
				width: 200upx;
				height: 50upx;
				border: 2upx solid #000;
				position: absolute;
				top: 30upx;
				right: 10upx;
				text-align: center;
				line-height: 50upx;
				box-sizing: border-box;
			}
		}
	}
</style>
